<template>
  <el-dialog :title="titleMap[mode]" v-model="visible" :width="600" destroy-on-close @closed="$emit('closed')">
    <el-form :model="form" :rules="rules" ref="dialogForm" label-width="150px">
      <el-form-item label="ID" prop="id" v-if="mode=='show'">
        <el-input v-model="form.id" placeholder="请输入ID" :readonly="mode=='show'" clearable></el-input>
      </el-form-item>
      <el-form-item label="任务名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入任务名称" :readonly="mode=='show'" clearable></el-input>
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
		<el-date-picker type="datetime" :format="this.$CONFIG.DATE_TIME_FORMAT_TEXT" :value-format="this.$CONFIG.DATE_TIME_FORMAT_VALUE" v-model="form.startTime" placeholder="选择时间或留空" :readonly="mode=='show'"></el-date-picker>
      </el-form-item>
      <el-form-item label="终止时间" prop="expireTime">
		<el-date-picker type="datetime" :format="this.$CONFIG.DATE_TIME_FORMAT_TEXT" :value-format="this.$CONFIG.DATE_TIME_FORMAT_VALUE" v-model="form.expireTime" placeholder="选择时间或留空" :readonly="mode=='show'"></el-date-picker>
      </el-form-item>
      <el-form-item label="执行器" prop="actuator">
        <el-input v-model="form.actuator" placeholder="请输入执行器" :readonly="mode=='show'" clearable></el-input>
		<el-link v-if="mode!='show'" type="primary" @click="openActuatorDialog">规则与案例</el-link>
      </el-form-item>
      <el-form-item label="触发规则" prop="schedule">
		<sc-cron v-model="form.schedule" placeholder="请输入触发规则" :disabled="mode=='show'" :shortcuts="shortcuts" clearable></sc-cron>
      </el-form-item>
      <el-form-item label="任务状态" prop="status">		
		<el-radio-group v-model="form.status" :disabled="mode=='show'">
			<el-radio-button label="ENABLED">启用</el-radio-button>
			<el-radio-button label="DISABLED">禁用</el-radio-button>
			<!-- <el-radio-button label="PAUSED">暂停</el-radio-button> -->
		</el-radio-group>
      </el-form-item>
      <el-form-item label="触发异常规则" prop="instruction">
		<el-radio-group v-model="form.instruction" :disabled="mode=='show'">
			<el-radio-button label="NEXT">跳过</el-radio-button>
			<el-radio-button label="NOW">立即执行</el-radio-button>
		</el-radio-group>
		<span>&nbsp;&nbsp;错过执行时间时的执行策略</span>
      </el-form-item>
      <el-form-item label="参数列表" prop="param">
        <el-input v-model="form.param" placeholder="请输入参数列表" :readonly="mode=='show'" clearable type="textarea" :autosize="{ minRows:10, maxRows:30 }"></el-input>
      </el-form-item>
      <el-form-item label="任务组" prop="groupName">
        <el-input v-model="form.groupName" placeholder="请输入任务群组" :readonly="mode=='show'" clearable></el-input>
      </el-form-item>
      <el-form-item label="最后运行开始时间" prop="runTimeStart" v-if="mode=='show'">
        <el-input v-model="form.runTimeStart" placeholder="请输入最后运行开始时间" readonly clearable></el-input>
      </el-form-item>
      <el-form-item label="最后运行结束时间" prop="runTimeEnd" v-if="mode=='show'">
        <el-input v-model="form.runTimeEnd" placeholder="请输入最后运行结束时间" readonly clearable></el-input>
      </el-form-item>
      <el-form-item label="下次触发时间" prop="nextTime" v-if="mode=='show'">
        <el-input v-model="form.nextTime" placeholder="请输入下次触发时间" readonly clearable></el-input>
      </el-form-item>
      <el-form-item label="最后调度时间" prop="loadTime" v-if="mode=='show'">
        <el-input v-model="form.loadTime" placeholder="请输入最后调度时间" readonly clearable></el-input>
      </el-form-item>
      <el-form-item :label="`创建时间(`+this.$CONFIG.DATE_TIME_ZONE_TEXT+`)`" prop="createTime" v-if="mode=='show'">
        <el-input v-model="form.createTime" placeholder="请输入创建时间" :readonly="mode=='show'" clearable></el-input>
      </el-form-item>
	  <el-form-item label="运行主机" prop="hostname">
        <el-input v-model="form.hostname" placeholder="请输入运行主机" :readonly="mode=='show'" clearable></el-input>
      </el-form-item>
	  <el-form-item label="运行环境" prop="envName">
        <el-input v-model="form.envName" placeholder="请输入运行环境" :readonly="mode=='show'" clearable></el-input>
      </el-form-item>
      <el-form-item label="运行描述" prop="runRemarks" v-if="mode=='show'">
        <el-input v-model="form.runRemarks" placeholder="请输入运行描述" readonly  type="textarea" :autosize="{ minRows:2, maxRows:6 }"></el-input>
      </el-form-item>
      <el-form-item label="任务描述" prop="description">
        <el-input v-model="form.description" placeholder="请输入描述" :readonly="mode=='show'" clearable type="textarea" :autosize="{ minRows:2, maxRows:6 }"></el-input>
      </el-form-item>
    </el-form>

    <template #footer v-if="mode!='show'">
      <el-button @click="visible=false" >取 消</el-button>
      <el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
    </template>

  </el-dialog>

  <actuatorDialog v-if="actuatorDialog" draggable @closed="actuatorDialog = false"></actuatorDialog>

</template>

<script>
    import setting from './setting'
	import scCron from '@/components/scCron'
	import actuatorDialog from './actuatorDialog'
	export default {
		emits: ['success', 'closed'],
		components: {
			scCron,
			actuatorDialog
		},
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					show: '查看'
				},
				actuatorDialog: false,
				visible: false,
				isSaveing: false,
				//
				shortcuts: [
					{text:'每60秒',value:'INTERVAL:60'},
					{text:'每600秒',value:'INTERVAL:600'}
				],
				//表单数据
				form: {
					id: 0,
					name: '',
					createTime: null,
					startTime: null,
					expireTime: null,
					groupName: '',
					description: '',
					actuator: '',
					schedule: '',
					instruction: 'NEXT',
					param: '',
					status: 'ENABLED',
					runStatus: '',
					runRemarks: '',
					runTimeStart: null,
					runTimeEnd: null,
					nextTime: null,
					loadTime: null,
					hostname: null,
					envName: null,
					_: ""
				},
				//验证规则
				/*
				rules: {
					sort: [
						{required: true, message: '请输入排序', trigger: 'change'}
					],
					name: [
						{required: true, message: '请输入名称'}
					]
				},
				*/
			}
		},
		mounted() {
		},
		methods: {
			openActuatorDialog(){
				this.actuatorDialog = true
			},
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res;
						if (this.mode == "edit"){
							const editObj = Object.assign({},this.form)
							editObj.createTime = null
							editObj.loadTime = null
							editObj.nextTime = null
							editObj.runStatus = null
							editObj.runTimeStart = null
							editObj.runTimeEnd = null
							res = await setting.api.edit.post(editObj)
						} else {
							res = await setting.api.add.post(this.form)
						}
						this.isSaveing = false;
						if(res.code == setting.successCode){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}
				})
			},
			//表单注入数据
			setData(data){
				this.form.id = data.id
				this.form.name = data.name
				this.form.createTime = data.createTime
				this.form.startTime = data.startTime
				this.form.expireTime = data.expireTime
				this.form.groupName = data.groupName
				this.form.description = data.description
				this.form.actuator = data.actuator
				this.form.schedule = data.schedule
				this.form.instruction = data.instruction
				this.form.param = data.param
				this.form.status = data.status
				this.form.runStatus = data.runStatus
				this.form.runRemarks = data.runRemarks
				this.form.runTimeStart = data.runTimeStart
				this.form.runTimeEnd = data.runTimeEnd
				this.form.nextTime = data.nextTime
				this.form.loadTime = data.loadTime
				this.form.hostname = data.hostname
				this.form.envName = data.envName

				//可以和上面一样单个注入，也可以像下面一样直接合并进去
				//Object.assign(this.form, data)
			}
		}
	}
</script>

<style>
</style>
